//index.js
var next_page = 1,
  page = 2;
//获取应用实例
const app = getApp()
var area_picker = require("./../../components/area-picker/area-picker.js");
Page({
  data: {
    aaa:false,
    class:"",
    multiIndex: [0, 0],
    multiArray: [
      ['北京', '安徽'],
      ['北京']
    ],
    no_show_district: true,
    objectMultiArray: [{}],
    region: {name: '全国',id: 0},
    // 用户
    user: {},
    // 消息小红点
    message: 0,
    statusbar: 0,
    // 微淘达人数据
    referrer: [],
    // 话题数据
    ask_answer: [],
    // 分类
    pitch: 0,
    listcont: [],
    list: [],
    article: {},
    searchData: '',
    is_detail_show: 0
  },
  zk:function(){
    this.setData({
      aaa:false,
      class:""
    })
  },
  sq:function(){
    this.setData({
      aaa:true,
      class:"aa"
    })
  },

  onLoad: function (t) {
    getApp().page.onLoad(this, t);
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    var s = this;
    wx.setNavigationBarTitle({
      title: '微淘'
    })
    const res = wx.getSystemInfoSync()
    var statusbarH = res.statusBarHeight
    this.setData({
      statusbar: statusbarH
    })
    var s = this;
    (getApp().core.getStorageSync('REGION') && s.setData({
      region: getApp().core.getStorageSync('REGION')
    })), this.loadData(t)
    s.getDistrictData(function (e) {
      area_picker.init({
        page: s,
        data: e
      });
    })
  },


  getLocation() {
    var t = this;
    wx.getLocation({
      success: function (a) {
        t.setData({
          longitude: a.longitude,
          latitude: a.latitude
        });
      },
      complete: function () {
        //t.locationContent();
      }
    });
  },
  locationContent: function () {
    // console.log(e)
    var o = this;
    getApp().request({
      url: getApp().api.mch.location_info,
      method: "GET",
      data: {
        longitude: o.data.longitude,
        latitude: o.data.latitude
      },
      success: function (t) {
        console.log(t)
        if (t.code == 0) {
          o.setData({
            region: {
              name: t.data.city.name,
              id: t.data.city.id
            }
          })
          o.loadData();
          console.log(t);
        } else {
          o.loadData();
        }

      },
      fail: function (t) {
        o.setData({
          myAddress: "定位失败2",
        })
      },
    });
  },

  getDistrictData: function (t) {
    var a = getApp().core.getStorageSync(getApp().const.DISTRICT);
    var address_china = {
      id: 0,
      name: "全国",
      parent_id: 0,
      level: 'province',
      list: [{
        id: 0,
        name: '全国',
        parent_id: 0,
        level: 'city',
        list: []
      }]
    }
    if (!a) return getApp().core.showLoading({
      title: "正在加载",
      mask: !0
    }), void getApp().request({
      url: getApp().api.default.district,
      success: function (e) {
        getApp().core.hideLoading(), 0 == e.code && (a = e.data, getApp().core.setStorageSync(getApp().const.DISTRICT, a),a.unshift(address_china),
          t(a));
      }
    });
    a.unshift(address_china)
    t(a);
  },
  onAreaPickerConfirm: function (e) {
    var region = {
      name: e[1].name,
      id: e[1].id
    }
    this.setData({
      region: region
    });
    getApp().core.setStorageSync('REGION', region),
      this.loadData();
  },
  loadData: function (t) { console.log('加载')
    var a = this;
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.index,
      data: {
        city_name: a.data.region.name,
        city_id: a.data.region.id,
        id: a.data.region.id
      },
      success: function (t) {
        console.log(t,'数据话题')
        0 == t.code && a.setData({
          list: t.data.listcont[a.data.pitch],
          user: t.data.user,
          referrer: t.data.referrer,
          ask_answer: t.data.ask_answer,
          listcont: t.data.listcont,
          message: t.data.message,
        });
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  loadData2: function (t) {
    var t = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.page_index,
      data: {
        city_name: t.data.region.name,
        city_id: t.data.region.id,
        search_type: t.data.pitch,
        page: page
      },
      success: function (e) {
        var i = t.data.list,
          s = e.data.list,
          n = !1;
        1000000000 == s.length && (n = 1), i = i.concat(s), page++, 
        t.setData({
          list: i
        });
        // next_page = n;

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  share_return() {
    return false;
  },
  bindRegionChange(e) {
    console.log(e.detail)
    this.setData({
      region: e.detail.value[1]
    })
    this.loadData();
  },
  but_tab(e) {
    this.setData({
      pitch: e.target.dataset.index,
      list: this.data.listcont[e.target.dataset.index],
    })
    next_page = 1, page = 2;
  },
  searchInput(e) {
    this.setData({
      searchData: e.detail.value
    });
  },
  searchConfirm() {
    if (!this.data.searchData) {
      getApp().core.showToast({
        title: "搜索内容不能为空",
        icon: 'none'
      });
      return;
    }
    var gopage = "/weitao/search_list/search_list?search_data=" + this.data.searchData;
    getApp().core.navigateTo({
      url: gopage
    });
  },
  onShareAppMessage: function (res) {
    getApp().page.onShareAppMessage(this);
    if (res.from === 'button') {
      var article = res.target.dataset.article;
      console.log(article);
      return {
        title: article.content,
        path: '/weitao/details/details?article_id=' + article.id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0),
        success: function (res) {
          console.log('成功', res)
        }
      }
    }
    return {
      title: '微淘首页',
      path: '/weitao/index/index',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
    return {
      title: '微淘首页',
      query: {
        key: value,
        store_id: wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0
      },
      imageUrl: ''
    }
  },
  praise(e) {
    // console.log(this)
    console.log(e)
  },
  /**
   * 
   * @param {*} e 
   * 关注
   */
  focus_action(e) {
    var user_id = e.currentTarget.dataset.userId,
      user_index = e.currentTarget.dataset.userIndex,
      s = this;
    var like_data = 0;
    s.data.referrer[user_index].is_user_liked == 1 ? like_data = 2 : like_data = 1;
    if (user_id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.focus_user,
        data: {
          user_id: user_id,
          is_user_liked: like_data
        },
        success: function (t) {
          0 == t.code && (s.data.referrer[user_index].is_user_liked = like_data) && a.setData({
            referrer: s.data.referrer
          });
          console.log(s.data.referrer[user_index]);
          getApp().core.showToast({
            title: t.msg,
            icon: 0 == t.code ? 'success' : 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
  like_action(e) {
    var list_index = e.currentTarget.dataset.listIndex,
      user_index = e.currentTarget.dataset.userIndex,
      s = this;
    var like_data = 0;
    console.log(s.data.list[list_index]);
    s.data.list[list_index].is_liked == 1 ? like_data = 0 : like_data = 1;
    let like_count = s.data.list[list_index].like_count;
    if (s.data.list[list_index].id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.like_article,
        data: {
          article_id: s.data.list[list_index].id,
          is_liked: like_data
        },
        success: function (t) {
          if (t.code == 0) {
            s.data.list[list_index].is_liked = like_data;
            if (like_data == 1) {
              s.data.list[list_index].like_count = parseInt(like_count) + 1;
            } else {
              if (parseInt(like_count) > 1) {
                s.data.list[list_index].like_count = parseInt(like_count) - 1;
              } else {
                s.data.list[list_index].like_count = 0;
              }
            }
            a.setData({
              list: s.data.list
            });
          }
          getApp().core.showToast({
            title: t.msg,
            icon: 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
  show_detail(e) {
    var article = {};
    if (e.currentTarget.dataset.article && this.data.is_detail_show != 1) {
      article = e.currentTarget.dataset.article;
      article.head_url = article.from_user_head;
      article.nickname = article.from_user_name;
      article.is_user_liked = article.is_focused;
    }
    this.setData({
      article: article,
      is_detail_show: this.data.is_detail_show == 1 ? 0 : 1
    });
    if (this.data.is_detail_show == 0) {
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#ff0000',
        animation: {
          duration: 400,
          timingFunc: 'easeIn'
        }
      })
      this.loadData();
    } else {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#ff0000',
        animation: {
          duration: 400,
          timingFunc: 'easeIn'
        }
      })
    }
    console.log(this.data.article)
  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url,
      s = this;
    if (!gopage) {
      return;
    }
    console.log(gopage, "去");
    wx.navigateTo({
      url: gopage,
      events: {
        someEvent: function (data) {
          console.log('返回了');
          s.loadData();
        }
      },
      success: function (res) {}
    })
  },
  bindMultiPickerChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1]
    })
  },
  bindMultiPickerColumnChange: function (e) {
    switch (e.detail.column) {
      case 0:
        list = []
        for (var i = 0; i < this.data.objectMultiArray.length; i++) {
          if (this.data.objectMultiArray[i].parid == this.data.objectMultiArray[e.detail.value].regid) {
            list.push(this.data.objectMultiArray[i].regname)
          }
        }
        this.setData({
          "multiArray[1]": list,
          "multiIndex[0]": e.detail.value,
          "multiIndex[1]": 0
        })

    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(next_page);
    next_page == 1 && this.loadData2()
  },
  onShareTimeline() {
    return {
      title: "微淘",
      path: "/pages/index/index"
    }
  }
})